# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=libproxy
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.5.3
pkgrel=1
pkgdesc="A library that provides automatic proxy configuration management (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://libproxy.github.io/libproxy/"
license=('spdx:LGPL-2.1-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2"
         "${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-duktape"
         "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
         "${MINGW_PACKAGE_PREFIX}-gsettings-desktop-schemas")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-vala")
checkdepends=("${MINGW_PACKAGE_PREFIX}-libsoup3")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/libproxy/libproxy/archive/refs/tags/${pkgver}.tar.gz"
        "0001-install-backend-bindir.patch")
sha256sums=('0d8d8e4dd96239ba173c2b18905c0bb6e161fd5000e1e0aeace16f754e9a9108'
            '659e9f5ab6097cff46b04e49b833c89422c2b7fbd6e26baf370fac7159c0aecf')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # Install libpxbackend-1.0.dll in bindir for libproxy-1.dll
  # Partially reverts https://github.com/libproxy/libproxy/pull/223
  patch -p1 -i "${srcdir}/0001-install-backend-bindir.patch"
}

build() {
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("--buildtype=release")
  else
    _extra_config+=("--buildtype=debug")
  fi

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson setup \
    --prefix="${MINGW_PREFIX}" \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    "${_extra_config[@]}" \
    -Ddocs=false \
    -Dtests=false \
    -Dconfig-kde=false \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/meson compile
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/meson test || warning "Tests failed"
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/meson install
  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
